<?php
/**
 * Created by JetBrains PhpStorm.
 * User: yanweidong
 * Date: 13-5-6
 * Time: 下午3:54
 * To change this template use File | Settings | File Templates.
 */
class user extends actionAbstract {


    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
        $this->partnerInfo = $this->userinfo=acl::checkLogin('PmsCookie','/index.php/partner/common/login');
        $this->partnerInfo['pmsId']=$this->partnerInfo['id'];
        if($this->partnerInfo['parentid']!=0){
        	$this->partnerInfo['id'] = $this->partnerInfo['parentid'];
        }
    }
    //本酒店信息
    function hotelziduan($ziduan){
        $this->loadModel('hotel','category');
        $re=$this->hotel->categoryModel->fetchRow("select $ziduan from hotel_category where id=".(isset($_SESSION['hotel_id'])?$_SESSION['hotel_id']:0)." and partnerid=".$this->partnerInfo['id']);
    	if($re){
        	return $re[$ziduan];
        }else{
        	return 0;
        }
    }
    
    //基本资料
    function basic(){
        global $pmsconfigs;
        $leftpost="user";
        $leftbot="basic";
    	$this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/basic");
        if(!$str_return){
    		$js->alert("您没有该权限！");exit();
    	}
        
        $this->loadModel('hotel','renovation');
        $this->loadModel('hotel','service');
        $this->loadModel('hotel','traffic');
        $this->loadModel('hotel','category');
        $hotelId=$this->hotelziduan('id');
        $rs=$this->hotel->categoryModel->fetchRow("select * from hotel_category where id=$hotelId");
        
        $renovation=$this->hotel->renovationModel->fetchAll("select * from hotel_renovation where status=1 and hotelId=".$rs['id']);
        $service=$this->hotel->serviceModel->fetchAll("select * from hotel_service where status=1 and hotelId=".$rs['id']);
        $traffic=$this->hotel->trafficModel->fetchAll("select traffic from hotel_traffic where hotelId=".$rs['id']." order by sort asc");
        
        $this->loadModel('tc','province');
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
        $province=$this->tc->provinceModel->fetchAll("select * from tc_province");
        $city=$this->tc->cityModel->fetchAll("select * from tc_city where province_id=".$rs['province']);
        $county=$this->tc->countyModel->fetchAll("select * from tc_county where city_id=".$rs['city']);
    	
        include $this->loadWidget('partnerTheme');
    }
    
    //详细资料
    function basicinfo(){
        global $pmsconfigs;
        $leftpost="user";
        $leftbot="basic";
    	$this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/basic");
        if(!$str_return){
    		$js->alert("您没有该权限！");exit();
    	}
        
        $this->loadModel('hotel','renovation');
        $this->loadModel('hotel','service');
        $this->loadModel('hotel','traffic');
        $this->loadModel('hotel','category');
        $hotelId=$this->hotelziduan('id');
        $rs=$this->hotel->categoryModel->fetchRow("select * from hotel_category where id=$hotelId");
        
        $renovation=$this->hotel->renovationModel->fetchAll("select * from hotel_renovation where status=1 and hotelId=".$rs['id']);
        $service=$this->hotel->serviceModel->fetchAll("select * from hotel_service where status=1 and hotelId=".$rs['id']);
        $traffic=$this->hotel->trafficModel->fetchAll("select traffic from hotel_traffic where hotelId=".$rs['id']." order by sort asc");
        
        $this->loadModel('tc','province');
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
        $province=$this->tc->provinceModel->fetchAll("select * from tc_province");
        $city=$this->tc->cityModel->fetchAll("select * from tc_city where province_id=".$rs['province']);
        $county=$this->tc->countyModel->fetchAll("select * from tc_county where city_id=".$rs['city']);
    	//echo "<pre>";print_r($renovation);exit;
        
        include $this->loadWidget('partnerTheme');
    }
    
    //基本资料修改
    function basicEdit(){
    	$this->loadHelper('extend');
        $this->loadHelper('image');
        $image = new image();
        $this->loadHelper('js');
    	$js=new js();
        
        global $pmsconfigs;
        $this->loadModel('hotel','category');
        $this->loadModel('tc','province');
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
        $this->loadModel('hotel','renovation');
        $this->loadModel('hotel','service');
        $this->loadModel('hotel','traffic');
        
        $str_return = $this->my_authority("user/basicEdit");
    	if(!$str_return){
    		ajaxReturn ('','您没有该权限！', 0 );exit;
    	}
        
        $hotelId=$this->hotelziduan('id');
        $rs=$this->hotel->categoryModel->fetchRow("select * from hotel_category where id=$hotelId");
        
        if($_POST){
            $data=isset($_POST['data'])?$_POST['data']:'';
            $renovation=isset($_POST['renovation'])?$_POST['renovation']:'';//装修风格
            $service=isset($_POST['service'])?$_POST['service']:'';//服务设施
            $traffic=isset($_POST['traffic'])?$_POST['traffic']:'';//交通距离
            
            if(empty($data['hotelName'])){
                ajaxReturn ('','请输入酒店名称！', 0 );exit;
            }elseif(empty($data['province'])){
                ajaxReturn ('','请选择省！', 0 );exit;
            }elseif(empty($data['city'])){
                ajaxReturn ('','请选择市！', 0 );exit;
            }elseif(empty($data['county'])){
                ajaxReturn ('','请选择区！', 0 );exit;
            }elseif(empty($data['address'])){
                ajaxReturn ('','请输入详细地址！', 0 );exit;
            }elseif(trim($data['phone'])==''){
                ajaxReturn ('','请输入正确的手机号！', 0 );exit;
            }elseif(trim($data['tel'])==''){
                ajaxReturn ('','请输入酒店电话！', 0 );exit;
            }
            
            if($this->partnerInfo['isbusiness']==1){
                if(trim($data['bank'])==''){
                    ajaxReturn ('','请填写开户行！', 0 );exit;
                }elseif(trim($data['contactsName'])==''){
                    ajaxReturn ('','请填写收款账户名称！', 0 );exit;
                }elseif(trim($data['contacts'])==''){
                    ajaxReturn ('','请填写账号！', 0 );exit;
                }
            }else{
                if(trim($data['contacts'])==''){
                    ajaxReturn ('','请输入银行对公收款账号！', 0 );exit;
                }
            }
            
            $re=$this->hotel->categoryModel->update($data,"id=".$rs['id']." and partnerid=".$this->partnerInfo['id']);
            if($re){
                /**
                 * 传输给OSII
                 */
                $provinceName=$this->tc->provinceModel->fetchRow("select name from tc_province where osiiid=".$data['province']);
                $cityName=$this->tc->cityModel->fetchRow("select name from tc_city where osiiid=".$data['city']);
                $countyName=$this->tc->countyModel->fetchRow("select name from tc_county where osiiid=".$data['county']);
                
                $this->loadApi('osiiHotel');
                $osiiHotel=new osiiHotel();
                $category = $this->hotel->categoryModel->fetchRow("SELECT * FROM hotel_category WHERE partnerid=".$this->partnerInfo['id']);
                
                $body=array();
                $body[0]['hotelCode']=$rs['id'];                    $body[0]['hotelName']=$rs['hotelName'];
                $body[0]['hotelEnName']="";                         $body[0]['hotelDescription']="";
                $body[0]['star']=$data['star'];                     $body[0]['provinceCode']=$data['province'];
                $body[0]['provinceName']=$provinceName['name'];     $body[0]['cityCode']=$data['city'];
                $body[0]['cityName']=$cityName['name'];             $body[0]['countyCode']=$data['county'];
                $body[0]['countyName']=$countyName['name'];         $body[0]['brief']="";
                $body[0]['openYear']=$category['practiceTime']==''?'2000-01-01':$category['practiceTime'];
                $body[0]['roomQuantity']=0;                         $body[0]['telephone']="";
                $body[0]['diyBreakfast']=0;                         $body[0]['chineseBreakfast']=0;
                $body[0]['westBreakfast']=0;                        $body[0]['fax']="";
				$body[0]['address']=$category['address'];
                
                $reosii=$osiiHotel->hotels($body);
                
                //酒店状态 传输
                $osiiStatus=0;
                if($category['status']==2){
                    $osiiStatus=1;
                }
                $bodys=array();
                $bodys['hotelCode']=$rs['id'];          $bodys['hotelStatus']=$osiiStatus;
                $hotelStatus_osii=$osiiHotel->setHotelStatus($bodys);
                
                if(isset($data['logo'])){
                    @unlink(ROOT_PATH.$_POST['hotellogo']);
                    if(strpos($_POST['hotellogo'],'_thumb')){
                        $old_log_thumb=str_replace('_thumb','',$_POST['hotellogo']);
                        @unlink(ROOT_PATH.$old_log_thumb);
                    }
                }
                ajaxReturn ('refurbish','操作成功！', 1 );exit;
            }
            ajaxReturn ('','操作失败！', 0 );exit;
        }
    }
    
    //酒店详情信息
    function basicinfoEdit(){
        $this->loadHelper('extend');
        $this->loadHelper('image');
        $image = new image();
        $this->loadHelper('js');
    	$js=new js();
        
        global $pmsconfigs;
        $this->loadModel('hotel','category');
        $this->loadModel('tc','province');
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
        $this->loadModel('hotel','renovation');
        $this->loadModel('hotel','service');
        $this->loadModel('hotel','traffic');
        
        $str_return = $this->my_authority("user/basicEdit");
    	if(!$str_return){
    		ajaxReturn ('','您没有该权限！', 0 );exit;
    	}
        
        $hotelId=$this->hotelziduan('id');
        $rs=$this->hotel->categoryModel->fetchRow("select * from hotel_category where id=$hotelId");
        
        if($_POST){
            $data=isset($_POST['data'])?$_POST['data']:'';
            $renovation=isset($_POST['renovation'])?$_POST['renovation']:'';//装修风格
            $service=isset($_POST['service'])?$_POST['service']:'';//服务设施
            $traffic=isset($_POST['traffic'])?$_POST['traffic']:'';//交通距离
            
            if(!is_numeric($data['maxNum'])||(int)($data['maxNum'])<1){
                ajaxReturn ('','容纳人数必须是大于0的整数！', 0 );exit;
            }elseif(!is_numeric($data['maxFloor'])||(int)($data['maxFloor'])<1){
                ajaxReturn ('','最大层高必须是大于0的整数！', 0 );exit;
            }elseif(!isset($data['meetNum'])){
                ajaxReturn ('','请选择是否有会议厅！', 0 );exit;
            }elseif(!isset($data['dinnerNum'])){
                ajaxReturn ('','请选择是否有宴会厅！', 0 );exit;
            }elseif(!is_numeric($data['guestNum'])||(int)($data['guestNum'])<1){
                ajaxReturn ('','客房数量必须是大于0的整数！', 0 );exit;
            }elseif(trim($data['practiceTime'])==''){
                ajaxReturn ('','请选择开业时间！', 0 );exit;
            }elseif(!is_numeric($data['parkNum'])||(int)($data['parkNum'])<1){
                ajaxReturn ('','停车容量必须是大于0的整数！', 0 );exit;
            }elseif(empty($renovation)){
                ajaxReturn ('','请选择装修风格！', 0 );exit;
            }
            
            if(!empty($_FILES['imagefile'])){
                $return_1 = $image->updImg('imagefile',600, 400);
                if (is_array($return_1)) {
                    ajaxReturn('', $return_1['text'], 0);exit;
                } else {
                    $data['logo'] = $return_1;
                }
            }else{
            	if(!isset($_POST['hotellogo'])||$_POST['hotellogo']==''){
            		ajaxReturn('', "请上传酒店主题片！", 0);exit;
            	}
            }
            
            if(!empty($_FILES['healthimg'])){
                $return_1 = $image->updImg('healthimg',600, 400);
                if (is_array($return_1)) {
                    ajaxReturn('', $return_1['text'], 0);exit;
                } else {
                    $data['healthimg'] = $return_1;
                }
            }else{
            	if(!isset($_POST['hotellogo'])||$_POST['hotellogo']==''){
            		ajaxReturn('', "请上传卫生许可证！", 0);exit;
            	}
            }
            
            if(!empty($_FILES['specialimg'])){
                $return_1 = $image->updImg('specialimg',600, 400);
                if (is_array($return_1)) {
                    ajaxReturn('', $return_1['text'], 0);exit;
                } else {
                    $data['specialimg'] = $return_1;
                }
            }else{
            	if(!isset($_POST['hotellogo'])||$_POST['hotellogo']==''){
            		ajaxReturn('', "请上传特种行业许可证！", 0);exit;
            	}
            }
            
            if(!empty($_FILES['safeimg'])){
                $return_1 = $image->updImg('safeimg',600, 400);
                if (is_array($return_1)) {
                    ajaxReturn('', $return_1['text'], 0);exit;
                } else {
                    $data['safeimg'] = $return_1;
                }
            }else{
            	if(!isset($_POST['hotellogo'])||$_POST['hotellogo']==''){
            		ajaxReturn('', "请上传特种行业许可证！", 0);exit;
            	}
            }
            
            if(!empty($_FILES['businessimg'])){
                $return_1 = $image->updImg('businessimg',600, 400);
                if (is_array($return_1)) {
                    ajaxReturn('', $return_1['text'], 0);exit;
                } else {
                    $data['businessimg'] = $return_1;
                }
            }else{
            	if(!isset($_POST['hotellogo'])||$_POST['hotellogo']==''){
            		ajaxReturn('', "请上传营业执照！", 0);exit;
            	}
            }
            $re=$this->hotel->categoryModel->update($data,"id=".$rs['id']." and partnerid=".$this->partnerInfo['id']);
            
            $i=0;
            if($renovation){
                $this->hotel->renovationModel->update("status=0","hotelId=$hotelId");
                $dataRenovation=array();
                foreach($renovation as $k=>$v){
                    $dataRenovation['hotelId']=$hotelId;
                    $dataRenovation['configureId']=$v;
                    $dataRenovation['status']=1;
                    
                    $th_renovation=$this->hotel->renovationModel->fetchRow("select id from hotel_renovation where hotelId=$hotelId and configureId=$v");
                    if($th_renovation){
                        $this->hotel->renovationModel->update("status=1","id=".$th_renovation['id']);
                        $i=$i+1;
                    }else{
                        $this->hotel->renovationModel->insert($dataRenovation);
                        $i=$i+1;
                    }
                }
            }
            if($re||$i>0){
                /**
                 * 传输给OSII
                 */
                $category = $this->hotel->categoryModel->fetchRow("SELECT * FROM hotel_category WHERE partnerid=".$this->partnerInfo['id']);
                $provinceName=$this->tc->provinceModel->fetchRow("select name from tc_province where osiiid=".$category['province']);
                $cityName=$this->tc->cityModel->fetchRow("select name from tc_city where osiiid=".$category['city']);
                $countyName=$this->tc->countyModel->fetchRow("select name from tc_county where osiiid=".$category['county']);
                
                $this->loadApi('osiiHotel');
                $osiiHotel=new osiiHotel();
                
                $body=array();
                $body[0]['hotelCode']=$rs['id'];                	$body[0]['hotelName']=$rs['hotelName'];
				$body[0]['hotelEnName']="";                     	$body[0]['hotelDescription']="";
                $body[0]['star']=$category['star'];             	$body[0]['provinceCode']=$category['province'];
				$body[0]['provinceName']=$provinceName['name'];     $body[0]['cityCode']=$category['city'];
                $body[0]['cityName']=$cityName['name'];         	$body[0]['countyCode']=$category['county'];
				$body[0]['countyName']=$countyName['name'];         $body[0]['brief']="";
                $body[0]['openYear']=$data['practiceTime'];     	$body[0]['roomQuantity']=0;
				$body[0]['telephone']="";                           $body[0]['diyBreakfast']=0;
                $body[0]['chineseBreakfast']=0;                 	$body[0]['westBreakfast']=0;
				$body[0]['fax']="";                                 $body[0]['address']=$category['address'];
                
                $reosii=$osiiHotel->hotels($body);
                
                //酒店状态 传输
                $osiiStatus=0;
                if($category['status']==2){
                    $osiiStatus=1;
                }
                $bodys=array();
                $bodys['hotelCode']=$rs['id'];          $bodys['hotelStatus']=$osiiStatus;
                $hotelStatus_osii=$osiiHotel->setHotelStatus($bodys);
                
                if(isset($data['logo'])){
                    @unlink(ROOT_PATH.$_POST['hotellogo']);
                    if(strpos($_POST['hotellogo'],'_thumb')){
                        $old_log_thumb=str_replace('_thumb','',$_POST['hotellogo']);
                        @unlink(ROOT_PATH.$old_log_thumb);
                    }
                }
                ajaxReturn ('refurbish','操作成功！', 1 );exit;
            }
            ajaxReturn ('','操作失败！', 0 );exit;
        }
    }
    
    //省市区选择
    function get_address(){
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
        $parentid=isset($_POST['parentid'])?(int)$_POST['parentid']:0;
        $type=isset($_POST['type'])?(int)($_POST['type']):0;
        if($type==1){
            $re=$this->tc->cityModel->fetchAll("select * from tc_city where province_id=$parentid");
        }elseif($type==2){
            $re=$this->tc->countyModel->fetchAll("select * from tc_county where city_id=$parentid");
        }
        $html="";
        $html.="<option value='0'>请选择</option>";
        if(!empty($re)){
            foreach($re as $k=>$v){
                $html.="<option value='".$v['osiiid']."'>".$v['name']."</option>";
            }
        }
        exit(json_encode(array('type'=>$type,'html'=>$html)));
    }
    
    //密码修改
    function password(){
        $leftpost="user";
        $leftbot="password";
    	$this->loadHelper('extend');
        $this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/password");
    	if(!$str_return){
    		$js->alert("您没有该权限！");exit();
    	}
        $this->loadModel('pms','account');
        if($_POST){
            $password=isset($_POST['password'])?md5($_POST['password']):'';
            $pwd=isset($_POST['pwd'])?$_POST['pwd']:'';
            $newpwd=isset($_POST['newpwd'])?$_POST['newpwd']:'';
            $rs=$this->pms->accountModel->fetchRow("select * from pms_account where password='".$password."' and id=".$this->partnerInfo['pmsId']);
            if($rs){
                if(empty($pwd)){
                    ajaxReturn ('','新密码不能为空！', 0 );exit;
                }elseif(!empty($pwd)&&$pwd!=$newpwd){
                    ajaxReturn ('','两次密码不一致！', 0 );exit;
                }elseif(!empty($pwd)&&$pwd==$newpwd){
                    $data['password']=md5($pwd);
                    $re=$this->pms->accountModel->update($data,$rs['id']);
                    if($re){
                        acl::delCookie('PmsCookie');
                        ajaxReturn ('back','保存成功！请重新登录！', 1 );exit;
                    }else{
                        ajaxReturn ('','保存失败！', 0 );exit;
                    }
                }
            }else{
                ajaxReturn ('','初始密码错误！', 0 );exit;
            }
        }
        
        include $this->loadWidget('partnerTheme');
    }
    
    //设施、设备页面
    function devices(){
    	$this->loadHelper('extend');
        $this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/devices");
    	if(!$str_return){
    		$js->alert("您没有该权限！");exit();
    	}
        $leftpost="user";
        $leftbot="devices";
        $this->loadHelper('pager');
        $hotelId=$this->hotelziduan('id');
        $this->loadModel('pms','devices');
        
        $page=isset($_GET['page'])?(int)($_GET['page']):1;
        $size = 10;
        $offset = ($page - 1) * $size;
        
        $reto=$this->pms->devicesModel->fetchAll("select * from pms_devices where hotelId=$hotelId");
        
        $count=count($reto);
        $number = ceil( $count / $size );
        $extend = new pager();
        $pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
		$re=array_slice($reto,$offset,$size);
		
        include $this->loadWidget('partnerTheme');
    }
	
    //添加、修改设施 设备
    function edit(){
    	$this->loadHelper('extend');
        $this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/edit");
    	if(!$str_return){
    		ajaxReturn ('','您没有该权限！', 0 );exit;
    	}
        $hotelId=$this->hotelziduan('id');
        $this->loadModel('pms','devices');
        $id=isset($_POST['id'])?(int)($_POST['id']):0;
        $data['title']=isset($_POST['title'])?$_POST['title']:'';
        $data['describes']=isset($_POST['describes'])?$_POST['describes']:'';
        
        if($id>0){
            $re=$this->pms->devicesModel->update($data,"id=$id and hotelId=$hotelId");
        }else{
            $data['hotelId']=$hotelId;
            $re=$this->pms->devicesModel->insert($data);
        }
        if($re){
            /**
             * 传输给OSII
             */
            $this->loadApi('osiiHotel');
            $osiiHotel=new osiiHotel();
            
            $body=array();
            $body[0]['hotelCode']=$hotelId;        				$body[0]['facilityId']=($id>0?$id:$re);
			$body[0]['facilityName']=$data['title'];			$body[0]['facilityDescript']=$data['describes'];
            
            $reosii=$osiiHotel->devices($body);
            ajaxReturn ('refurbish','操作成功！', 1 );exit;
            
        }else{
            ajaxReturn ('','操作失败！', 0 );exit;
        }
    }
	
    //删除 设施 设备
    function del(){
    	$this->loadHelper('extend');
        $this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/del");
    	if(!$str_return){
    		exit(json_encode(array('state'=>0,'html'=>'您没有该权限！')));
    	}
        $this->loadModel('pms','devices');
        $hotelId=$this->hotelziduan('id');
        $id=isset($_POST['id'])?(int)($_POST['id']):0;
        $re=$this->pms->devicesModel->delete("id=$id and hotelId=$hotelId");
        if($re){
            exit(json_encode(array('state'=>1,'html'=>'操作成功！')));
        }
        exit(json_encode(array('state'=>0,'html'=>'操作失败！')));
    }
    
    //账号管理
    function account(){
        $leftpost="user";
        $leftbot="account";
    	$this->loadHelper('extend');
        $this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/account");
    	if(!$str_return){
    		$js->alert("您没有该权限！");exit();
    	}
    	$this->loadModel('sys','role');
    	$this->loadModel('pms','account');
    	$this->loadHelper('pager');
        $hotelId=$this->hotelziduan('id');
        
    	$page=isset($_GET['page'])?(int)($_GET['page']):1;
		
    	$size = 10;
    	$offset = ($page - 1) * $size;
		
		$db=$this->sys->roleModel->fetchAll("select * from sys_role where status=1 and hotelId=".$this->partnerInfo['id']);
		$sql="select a.*,b.title from pms_account as a left join sys_role as b on b.id=a.roleid";
		$sql.=" where a.parentid=".$this->partnerInfo['id'];
    	$reto=$this->pms->accountModel->fetchAll($sql);
		
    	$count=count($reto);
    	$number = ceil( $count / $size );
    	$extend = new pager();
    	$pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
		$re=array_slice($reto,$offset,$size);
		
    	include $this->loadWidget('partnerTheme');
    }
    
    //添加、修改子账号
    function edit_account(){
    	$this->loadHelper('extend');
        $this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/edit_account");
    	if(!$str_return){
    		ajaxReturn ('','您没有该权限！', 0 );exit;
    	}
        
    	$this->loadModel('pms','account');
    	$id = isset($_POST['id'])?(int)($_POST['id']):0;
    	$data = isset($_POST['data'])?$_POST['data']:'';
    	$password = isset($_POST['password'])?$_POST['password']:'';
        $data['status']=isset($_POST['status'])?$_POST['status']:1;
        
        if(!isset($data['roleid'])||empty($data['roleid'])){
            ajaxReturn ('','请选择角色，没有角色请先添加角色！', 0 );exit;
        }
		if(trim($data['userName'])==''){
            ajaxReturn ('','请填写账号！', 0 );exit;
        }
		if(empty($password)){
            ajaxReturn ('','请填密码！', 0 );exit;
        }
		$acre=$thsi->pms->accountModel->fetchRow("select id from pms_account where userName='".$data['userName']."' and id!=$id");
		if($acre){
			ajaxReturn ('','该账号已存在！', 0 );exit;
		}
		
        $data['password']=md5($password);
        $data['parentid']=$this->partnerInfo['id'];
        $data['isbusiness']=$this->partnerInfo['isbusiness'];
        
    	if($id>0){
    		$re=$this->pms->accountModel->update($data,"id=$id");
    	}else{
			$data['created']=time();
    		$re=$this->pms->accountModel->insert($data);
    	}
    	if($re){
            ajaxReturn ('refurbish','操作成功！', 1 );exit;
    	}else{
            ajaxReturn ('','操作失败！', 0 );exit;
    	}
    }
	
    //删除 子账号
    function del_account(){
    	$this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/del_account");
    	if(!$str_return){
    		exit(json_encode(array('state'=>0,'html'=>'您没有该权限！')));
    	}
    	$this->loadModel('pms','account');
    	$id=isset($_POST['id'])?(int)($_POST['id']):0;
    	$re=$this->pms->accountModel->delete("id=$id");
    	if($re){
    		exit(json_encode(array('state'=>1,'html'=>'操作成功！')));
    	}
    	exit(json_encode(array('state'=>0,'html'=>'操作失败！')));
    }
    
    /**
     * 角色列表页
     */
    function role(){
        $leftpost="user";
        $leftbot="role";
        $hotelId=$this->hotelziduan('id');
        $this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/role");
    	if(!$str_return){
    		$js->alert("您没有该权限！");exit();
    	}
        $this->loadModel('sys','role');
        $this->loadHelper('pager');
        
        $page=isset($_GET['page'])?(int)($_GET['page']):1;
        $size = 7;
        $offset = ($page - 1) * $size;
		
        $reto = $this->sys->roleModel->fetchAll("SELECT * FROM sys_role where hotelId=".$this->partnerInfo['id']);
        
        $count=count($reto);
        $number = ceil( $count / $size );
        $extend = new pager();
        $pageHtml = $extend->outputadmin( $number, $page, "", "", $count, $size );
		$re=array_slice($reto,$offset,$size);
		
        include $this->loadWidget('partnerTheme');
    }
    
    function getSon($categoryID,$id) { 
        //@初始化栏目数组 
        $this->loadModel('sys','menu');
        $this->loadModel('sys','rolepriv');
        $array[] = $categoryID; 
        do {
			$ids = '';
			if($id==0){
				$temp = $this->sys->menuModel->fetchAll('SELECT * FROM `sys_menu`  WHERE parentid IN (' . $categoryID . ') and isshow=1 and state=1'); 
			}else{
				$temp = $this->sys->menuModel->fetchAll('SELECT a.*,b.status FROM `sys_menu` AS a LEFT JOIN sys_rolepriv AS b ON a.menuid=b.menuId WHERE a.parentid IN (' . $categoryID . ') AND b.roleId='.$id.' and isshow=1 and state=1'); 
			}
			
			foreach ($temp as $v) {
				$array[] = $v['menuid']; 
				$son[] = $v;
				$ids .= ',' . $v['menuid']; 
			}
			$ids = substr($ids, 1, strlen($ids)); 
			$categoryID = $ids; 
        } 
        while (!empty($temp));
		
        return $son; 
    }
	
    /**
     * 添加或者修改角色信息
     */
    function roleEdit(){
        $leftpost="user";
        $leftbot="role";
        $this->loadHelper('extend');
        $hotelId=$this->hotelziduan('id');
        
        $this->loadHelper('extend');
        $this->loadHelper('js');
    	$js=new js();
    	$str_return = $this->my_authority("user/roleEdit");
    	if(!$str_return){
    		$js->alert("您没有该权限！");exit();
    	}
        $this->loadModel('sys','role');
        $this->loadModel('sys','menu');
        $this->loadModel('sys','rolepriv');
        $id=isset($_GET['id'])?(int)($_GET['id']):0;
        $menu = $this->sys->menuModel->fetchAll('SELECT * FROM sys_menu WHERE parentid=0');
        
        //显示页面
        if($_GET){
            $re=$this->sys->roleModel->fetchRow("select * from sys_role where id=$id");
        }
        foreach ($menu as $k=>$v){
           $menu[$k]['son'] = $this->getSon($v['menuid'],$id);
        }
        //echo '<pre>';print_r($menu);exit;
        //修改
        if($_POST){
            $data=isset($_POST['data'])?$_POST['data']:'';
            if(empty($data['title'])){
                ajaxReturn ('','角色名称不能为空！', 0 );exit;
            }
            
            $menuArr = isset($_POST['menu'])?$_POST['menu']:'';
            $id=isset($_POST['id'])?$_POST['id']:0;
			$sql="select * from sys_role where hotelId='".$this->partnerInfo['id']."' and title='".$data['title']."' and id!=$id";
            $setname=$this->sys->roleModel->fetchRow($sql);
            
            if($setname){
                ajaxReturn ('','该角色已存在！', 0 );exit;
            }
            $data['hotelId']=$this->partnerInfo['id'];
            //修改
            if($id){
                $data['created'] = date('Y-m-d H:i:s',time());
                $re=$this->sys->roleModel->update($data,$id);
                $this->sys->roleprivModel->delete('roleId='.$id);
                foreach ($menuArr as $k=>$v){
                    $arr = array(
                        'title'=>$v['title'],
                        'roleId'=>$id,
                        'menuId'=>$k,
                        'status'=>empty($v['status'])?1:$v['status']
                    );
                    $this->sys->roleprivModel->insert($arr);
                }
            //添加
            }else{
                $re=$this->sys->roleModel->insert($data);
                $roleId = $this->sys->roleModel->insertId();
                foreach ($menuArr as $k=>$v){
                    $arr = array(
                        'title'=>$v['title'],
                        'roleId'=>$roleId,
                        'menuId'=>$k,
                        'status'=>empty($v['status'])?1:$v['status']
                    );
                    $this->sys->roleprivModel->insert($arr);
                }
            }
            if($re){
                ajaxReturn ('back','操作成功！', 1 );exit;
            }else{
                ajaxReturn ('','操作失败！', 0 );exit;
            }
        }
        
        include $this->loadWidget('partnerTheme');
    }

    /**
     * 删除角色
     */
    function roleDel(){
        $leftpost="user";
        $leftbot="roleDel";
        
        $str_return = $this->my_authority("user/roleDel");
    	if(!$str_return){
    		exit(json_encode(array('state' => 0, 'html' => '您没有该权限！')));
    	}
        
        $this->loadModel('sys','role');
        $this->loadModel('sys','rolepriv');
        if($_POST){
            $id=isset($_POST['id'])?$_POST['id']:0;
            $this->sys->roleprivModel->delete('roleId='.$id);
            $re=$this->sys->roleModel->delete($id);
            if($re){
                exit(json_encode(array('state' => 1, 'html' => '操作成功！')));
            }else{
                exit(json_encode(array('state' => 0, 'html' => '操作失败！')));
            }
        }
    }
    
    /**
     * 修改角色状态
     */
    function roleStatus(){
        $leftpost="user";
        $leftbot="roleStatus";
        
        $str_return = $this->my_authority("user/roleStatus");
    	if(!$str_return){
    		exit(json_encode(array('state' => 0, 'html' => '您没有该权限！')));
    	}
        
        $this->loadModel('sys','role');
        if($_POST){
            $id=isset($_POST['id'])?$_POST['id']:0;
            $sta=isset($_POST['status'])?$_POST['status']:1;
            $arr = array('status'=>$sta);
            $re=$this->sys->roleModel->update($arr,$id);
            if($re){
                exit(json_encode(array('state' => 1, 'html' => '操作成功！')));
            }else{
                exit(json_encode(array('state' => 0, 'html' => '操作失败！')));
            }
        }
    }
    
    
    
    
}